package core.proto;

import app.exceptions.CollabVizException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

public class InfoViz extends JApplet {
    /**
     *
     */
    private static final long serialVersionUID = 1L;

    int width = 1280;

    int height = 960;

    String configPath;

    JComboBox viewList;

    JCheckBox screenCapOption;


    public void init() {
        try {
            SwingUtilities.invokeAndWait( new Runnable() {
                public void run() {
                    JPanel uploadPanel = new JPanel( new FlowLayout( FlowLayout.CENTER ) );
                    uploadPanel.setSize( width, height );
                    //add instructions to upload public key
                    final JLabel uploadInstruct = new JLabel( "Please select the config file" );
                    uploadPanel.add( uploadInstruct );

                    //add the textfield for the filename
                    final JTextField fileField = new JTextField( 25 );
                    fileField.setText( "Click browse to select your config file" );
                    fileField.setEditable( false );
                    uploadPanel.add( fileField );

                    //create the upload button but don't add it yet.
                    final JButton startButton = new JButton( "Start" );
                    startButton.addActionListener( new StartButtonHandler() );
                    startButton.setEnabled( false );

                    //add the search button
                    final JFileChooser fc = new JFileChooser();
                    final JButton browseButton = new JButton( "Browse..." );
                    browseButton.addActionListener( new ActionListener() {
                        @Override
                        public void actionPerformed( ActionEvent e ) {
                            int returnVal = fc.showOpenDialog( fileField );
                            if( returnVal == JFileChooser.APPROVE_OPTION ) {

                                File configFile = fc.getSelectedFile();
                                String fileName = configFile.toString();
                                fileField.setText( fileName );
                                if( fileName.endsWith( "properties" ) ) {
                                    startButton.setEnabled( true );
                                    configPath = fileName;
                                }


                            }

                        }
                    } );


                    //add the upload button
                    uploadPanel.add( browseButton );
                    uploadPanel.add( startButton );

                    String[] viewStrings = { "Complete View", "No Viz View", "No Notes View", "No Viz/Notes View" };

                    //Create the combo box, select item at index 4.
                    //Indices start at 0, so 4 specifies the pig.
                    viewList = new JComboBox( viewStrings );
                    viewList.setSelectedIndex( 0 );
                    screenCapOption = new JCheckBox( "Enable Screen Capture" );
                    uploadPanel.add( viewList );
                    uploadPanel.add( screenCapOption );
                    add( uploadPanel );
                    setSize( width, height );
                }
            } );
        } catch( Exception e ) {
            System.err.println( "createGUI didn't complete successfully" );
        }


    }

    private class StartButtonHandler implements ActionListener {

        @Override
        public void actionPerformed( ActionEvent arg0 ) {
            // TODO Auto-generated method stub
            try {
                getContentPane().removeAll();
                setSize( width, height );
                getContentPane().add( new AllComponents( configPath, viewList.getSelectedIndex(),
                        screenCapOption.isSelected() ) );

            } catch( IOException e ) {
                e.printStackTrace();
            } catch( CollabVizException e ) {
                e.printStackTrace();
            }
        }

    }

}
